<script setup lang="ts">
import { useRouter } from "vue-router";
import { computed, onBeforeMount, reactive } from "vue";
import { useGlobal, deviceDetection } from "@pureadmin/utils";
import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";
import LaySidebarTopCollapse from "@/layout/components/lay-sidebar/components/SidebarTopCollapse.vue";

import LayUserPane from "@/layout/components/lay-user/index.vue";
import Profile from "./components/Profile.vue";
import Preferences from "./components/Preferences.vue";
import Logs from "./components/Logs.vue";

import leftLine from "~icons/ri/arrow-left-s-line";
import ProfileIcon from "~icons/ri/user-3-line";
import PreferencesIcon from "~icons/ri/settings-3-line";
import LogsIcon from "~icons/ri/window-line";

defineOptions({
  name: "UserCenter"
});

const { $storage } = useGlobal<GlobalPropertiesApi>();

const router = useRouter();

// 状态参数
const state = reactive({
  witchPane: "profile",
  isOpen: deviceDetection() ? false : true
});

const panes = [
  {
    key: "profile",
    label: "个人信息",
    icon: ProfileIcon,
    component: Profile
  },
  {
    key: "preferences",
    label: "账号设置",
    icon: PreferencesIcon,
    component: Preferences
  },
  {
    key: "logs",
    label: "安全日志",
    icon: LogsIcon,
    component: Logs
  }
];

const handleChangePanes = (key: string) => {
  state.witchPane = key;
  if (deviceDetection()) {
    state.isOpen = !state.isOpen;
  }
};

const current = computed(
  () => panes.find(item => item.key === state.witchPane).label
);

onBeforeMount(() => {
  useDataThemeChange().dataThemeChange($storage.layout?.overallStyle);
});
</script>

<template>
  <el-container class="h-full">
    <el-aside
      v-if="state.isOpen"
      class="pure-account-settings overflow-hidden px-2 dark:!bg-[var(--el-bg-color)] border-r-[1px] border-[var(--pure-border-color)]"
      :width="deviceDetection() ? '180px' : '240px'"
    >
      <el-menu
        :default-active="state.witchPane"
        class="pure-account-settings-menu"
      >
        <el-menu-item
          index="back"
          class="hover:!transition-all hover:!duration-200 hover:!text-base !h-[50px]"
          @click="router.go(-1)"
        >
          <div class="flex items-center">
            <IconifyIconOffline :icon="leftLine" />
            <span class="ml-2">返回</span>
          </div>
        </el-menu-item>
        <el-menu-item
          v-for="item in panes"
          :key="item.key"
          :index="item.key"
          @click="handleChangePanes(item.key)"
        >
          <div class="flex items-center z-10">
            <el-icon><IconifyIconOffline :icon="item.icon" /></el-icon>
            <span>{{ item.label }}</span>
          </div>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-main class="p-0!">
      <LaySidebarTopCollapse
        v-if="deviceDetection()"
        class="px-0"
        :is-active="state.isOpen"
        @toggleClick="state.isOpen = !state.isOpen"
      />
      <div
        class="flex items-center justify-between border-b-[1px] border-[var(--pure-border-color)]"
      >
        <el-breadcrumb class="ml-4" separator="/">
          <el-breadcrumb-item>用户中心</el-breadcrumb-item>
          <el-breadcrumb-item>{{ current }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div>
          <LayUserPane class="vertical-header-right" user-center />
        </div>
      </div>
      <div class="p-5 pl-4 pr-4">
        <component
          :is="panes.find(item => item.key === state.witchPane).component"
          :class="[
            !deviceDetection() && 'ml-[120px]',
            'min-w-[180px]',
            'mt-12',
            deviceDetection() ? 'max-w-[100%]' : 'max-w-[70%]'
          ]"
        />
      </div>
    </el-main>
  </el-container>
</template>

<style lang="scss">
.pure-account-settings {
  background: var(--pure-theme-menu-bg);
}

.pure-account-settings-menu {
  background-color: transparent;
  border: none;

  .el-menu-item {
    height: 48px !important;
    color: var(--pure-theme-menu-text);
    background-color: transparent !important;
    transition: color 0.2s;

    &:hover {
      color: var(--pure-theme-menu-title-hover) !important;
    }

    &.is-active {
      color: #fff !important;

      &:hover {
        color: #fff !important;
      }

      &::before {
        position: absolute;
        inset: 0 8px;
        clear: both;
        margin: 4px 0;
        content: "";
        background: var(--el-color-primary);
        border-radius: 3px;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
body[layout] {
  .el-menu--vertical .is-active {
    color: #fff !important;
    transition: color 0.2s;

    &:hover {
      color: #fff !important;
    }
  }
}
</style>
